//
//  ViewController.swift
//  SmartCheckIn
//
//  Created by CidyChou on 2017/8/14.
//  Copyright © 2017年 CidyChou. All rights reserved.
//

import UIKit
import Foundation
//import SwiftHTTP
class ViewController: UIViewController,UITextFieldDelegate{
    
    var navigationBar : UINavigationBar!
    
    //var txtUser:UITextField!
    var picker:UIPickerView!
    var server:[String]!
    let alertController:UIAlertController=UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.actionSheet)
    
    @IBOutlet weak var tb_title: UIBarButtonItem!
    @IBOutlet weak var tfPhone: UITextField!
    @IBOutlet weak var btnNext: UIButton!
    
    override func viewDidLoad() {
        
        tfPhone.delegate = self
        
        //状态栏
        UIApplication.shared.statusBarStyle = UIStatusBarStyle.lightContent
        //color
        //UIColor(red: 55/255, green: 186/255, blue: 89/255, alpha: 0.5)
        //UIColor(red: 0x37/255, green: 0xba/255, blue: 0x46/255, alpha: 0.5)
        setStatusBarBackgroundColor(color: UIColor(red: 27/255, green: 130/255, blue: 210/255, alpha: 1))
        
        navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 20, width: self.view.bounds.size.width, height: 44))
        navigationBar.barTintColor = UIColor(red: 27/255, green: 130/255, blue: 210/255, alpha: 1)
        let dict:NSDictionary = [NSForegroundColorAttributeName: UIColor.white,NSFontAttributeName : UIFont.boldSystemFont(ofSize: 18)]
        navigationBar.titleTextAttributes = dict as? [String : AnyObject]
        navigationBar.tintColor = UIColor.gray
        //end
        
        let navigationItem = UINavigationItem(title: "手机验证")
        
        navigationBar.pushItem(navigationItem, animated: true)
        self.view.addSubview(navigationBar)
        
        btnNext.backgroundColor = UIColor.gray
        btnNext.setTitleColor(UIColor.white, for: .normal)
        btnNext.isEnabled = false
        
        super.viewDidLoad()
    }
    
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        let currentText = textField.text ?? ""
        let newText = (currentText as NSString).replacingCharacters(in: range, with: string)
        
        if RegexUtils().doMatch(input: newText,regex: "^1[0-9]{10}$"){
                btnNext.isEnabled = true
                btnNext.backgroundColor = UIColor(red: 27/255, green: 130/255, blue: 210/255, alpha: 1)
        }else{
            btnNext.isEnabled = false
            btnNext.backgroundColor = UIColor.gray
        }

        return true
    }
    
    @IBAction func LoginBtn(_ sender: Any) {
        print("123")
        //self.picker.isHidden = false
        
        //        do {
        //            //https://api.ucpaas.com/{SoftVersion}/Accounts/{accountSid}/{function}/{operation}?sig={SigParameter}
        //
        //
        //            let params = ["param": "param1",
        //
        //                          "array": ["first array element","second","third"],
        //
        //                          "num": 23,
        //
        //                          "dict": ["someKey": "someVal"]] as [String : Any]
        //
        //            do {
        //                //https://api.ucpaas.com
        //
        //                let opt = try HTTP.POST("http://www.111cn.net", parameters: params)
        //
        //                opt.start { response in
        //
        //                    //进行具体的响应操作...
        //
        //                }
        //
        //            } catch let error {
        //
        //                print("请求失败: \(error)")
        //
        //            }}
        
        //        clientSocket = GCDAsyncSocket()
        //        clientSocket.delegate = self
        //        clientSocket.delegateQueue = DispatchQueue.global()
        //        do {
        //            try clientSocket.connect(toHost: serveripInput.text!, onPort: serverPort)
        //        } catch {
        //            print("error")
        //            conBtn.backgroundColor = UIColor.red
        //        }
        
        
        //if TF_Account.text?.isEqual("123") && TF_Password?.isEqual("123"){
        
        performSegue(withIdentifier: "login", sender: self)
        
        // }
        
    }
    
    
    /// 设置状态栏颜色
    func setStatusBarBackgroundColor(color : UIColor) {
        let statusBarWindow : UIView = UIApplication.shared.value(forKey: "statusBarWindow") as! UIView
        let statusBar : UIView = statusBarWindow.value(forKey: "statusBar") as! UIView
        /*
         if statusBar.responds(to:Selector("setBackgroundColor:")) {
         statusBar.backgroundColor = color
         }*/
        if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
            statusBar.backgroundColor = color
        }
    }
    
    
//    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
//        return server[row]
//    }
//    
//    //当用户选中指定列的列表项时激发该方法
//    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//        print(server[row])
//    }
//    
//    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//        return server.count
//    }
//    
//    //返回1 表示只显示一列
//    func numberOfComponents(in pickerView: UIPickerView) -> Int {
//        return 1
//    }
}

